package whu.cn.whushare.ui.main.search;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.Constants;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;
import whu.cn.whushare.ui.common.BaseActivity;
import whu.cn.whushare.ui.common.LoadingHelper;
import whu.cn.whushare.ui.main.known.adapter.SearchUserAdapter;
import whu.cn.whushare.util.SetOnClickUtil;
import whu.cn.whushare.util.Utils;

/**
 * 文件名：SearchUserFragment
 * 描 述：
 * 作 者：胡平平
 * 时 间：2015/4/9
 */
@ViewMapping(id = R.layout.activity_invite_answer)
public class SearchUserActivity extends BaseActivity {
    @ViewMapping(id = R.id.refresh_list)
    private PullToRefreshListView mRefreshListView;

    private LoadingHelper mLoadingHelper;
    private int mCurPage = 1;
    private String mQuery = "";
    private List<User> mUsers = new ArrayList<>();
    private String mQuestionId;
    private SearchUserAdapter mAdapter;

    public static Intent getStartIntent(Context context) {
        Intent intent = new Intent(context, SearchUserActivity.class);

        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    //初始化控件
    private void initView() {
        View root = ViewMappingUtil.mapView(this, this);
        getTopBar(root);
        SetOnClickUtil.registerOnClick(this);

        mLoadingHelper = new LoadingHelper(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestInvite(mCurPage);
            }
        });
        mLoadingHelper.onCreateView(LayoutInflater.from(this), mRefreshListView);

        mTopBar.searchLayout.setVisibility(View.VISIBLE);
        mTopBar.searchTxt.setText(R.string.search);
        mTopBar.searchTxt.setOnClickListener(this);
        mTopBar.searchView.setOnClickListener(this);

        mAdapter = new SearchUserAdapter(mUsers, this);
        mRefreshListView.setAdapter(mAdapter);
        mRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);

        mRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                if (mTotal <= mUsers.size()) {
                    Utils.setRefreshLabelToLast(SearchUserActivity.this, mRefreshListView);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mRefreshListView.onRefreshComplete();
                        }
                    }, 500);
                } else {
                    requestInvite(mCurPage + 1);
                }
            }
        });

        mRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                User user = mAdapter.getItem(position - 1);

            }
        });
    }


    private void requestInvite(int page) {
        mLoadingHelper.showLoadingView(false);
        Downloader.getInstance(this).searchUser(mQuery, page, 20, this, page);
    }



    @Override
    public <T> void onSuccess(DownloadParams.PARSING_TYPE type, T response, Object obj) {
        super.onSuccess(type, response, obj);
        switch (type) {
            case SEARCH_USER:
                mRefreshListView.onRefreshComplete();
                mLoadingHelper.showContentView();
                if (response != null) {
                    mCurPage = (int) obj;
                    updateViews((List<User>) response);
                }
                break;
            case INVITE:
                toast(msg);
                finish();
                break;
        }
    }

    @Override
    public void onFail(DownloadParams.PARSING_TYPE type, VolleyError error) {
        super.onFail(type, error);
        switch (type) {
            case SEARCH_USER:
                mRefreshListView.onRefreshComplete();
                if (mCurPage == 1) {
                    mLoadingHelper.showRetryView();
                } else {
                    mLoadingHelper.showContentView();
                }
                break;
            case INVITE:
                finish();
        }

        toast(msg);
    }

    private void updateViews(List<User> users) {
        if (mCurPage == 1) {
            mUsers.clear();
            Utils.resetRefreshLabel(this, mRefreshListView);
        }

        mUsers.addAll(users);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.searchTxt:
                mQuery = mTopBar.searchView.getQuery().toString();
                if (TextUtils.isEmpty(mQuery)) {
                    toast(R.string.no_query);
                } else {
                    requestInvite(1);
                }
                break;
            default:
                break;
        }
    }
}

